/**   
 * Copyright © 2018 HIT akeil. Tech Ltd. All rights reserved.
 * 
 * 功能描述：
 * @Package: com.akeil.ui.dialogs 
 * @author: snow   
 * @date: 2018年8月8日 下午11:31:11 
 */
package com.akeil.flash.dialog;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**   
* Copyright: Copyright (c) 2018 HIT akeil
* 
* @ClassName: ChangePasswordDialog.java
* @Description: 该类的功能描述
*
* @version: v1.0.0
* @author: snow
* @date: 2018年8月8日 下午11:31:11 
*
* Modification History:
* Date         Author          Version            Description
*---------------------------------------------------------*
* 2018年8月8日     snow           v1.0.0               修改原因
*/
public class ChangePasswordDialog extends TitleAreaDialog {
	private Text textUsername;
	private Text textOldPassword;
	private Text textNewPassword;
	private Text textNewPasswordAgain;
	
	private String username = null;
	private String password = null;
	private String newpassword = null;
	private String newpasswordagain = null;

	public ChangePasswordDialog(Shell parentShell) {
		super(parentShell);
		setHelpAvailable(false);
		// TODO Auto-generated constructor stub
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getNewpassword() {
		return newpassword;
	}

	public void setNewpassword(String newpassword) {
		this.newpassword = newpassword;
	}

	public String getNewpasswordagain() {
		return newpasswordagain;
	}

	public void setNewpasswordagain(String newpasswordagain) {
		this.newpasswordagain = newpasswordagain;
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		// TODO Auto-generated method stub
		Composite composite = (Composite)super.createDialogArea(parent);
		GridLayout gridLayout = (GridLayout) composite.getLayout();
		
		Composite composite_1 = new Composite(composite, SWT.NONE);
		GridLayout gl_composite_1 = new GridLayout(2, false);
		gl_composite_1.marginRight = 20;
		gl_composite_1.marginLeft = 20;
		gl_composite_1.verticalSpacing = 10;
		gl_composite_1.marginTop = 10;
		gl_composite_1.marginHeight = 10;
		gl_composite_1.marginBottom = 5;
		composite_1.setLayout(gl_composite_1);
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		Label label = new Label(composite_1, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label.setText("用户名");
		
		textUsername = new Text(composite_1, SWT.BORDER | SWT.READ_ONLY);
		textUsername.setEditable(false);
		textUsername.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		if(username != null){
			textUsername.setText(username);
		}
		
		Label label_1 = new Label(composite_1, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_1.setText("原密码");
		
		textOldPassword = new Text(composite_1, SWT.BORDER | SWT.PASSWORD);
		textOldPassword.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		if(password != null){
			textOldPassword.setText(password);
		}
		
		Label label_2 = new Label(composite_1, SWT.NONE);
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_2.setText("输入新密码");
		
		textNewPassword = new Text(composite_1, SWT.BORDER | SWT.PASSWORD);
		textNewPassword.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				if(textNewPassword.getText().trim().equals("")){
					return;
				}
				
				if(textNewPassword.getText().trim().equals(textNewPasswordAgain.getText().trim())){
					ChangePasswordDialog.this.setErrorMessage(null);
					ChangePasswordDialog.this.getButton(IDialogConstants.OK_ID).setEnabled(true);
				}else{
					ChangePasswordDialog.this.getButton(IDialogConstants.OK_ID).setEnabled(false);
					ChangePasswordDialog.this.setErrorMessage("两次输入的密码不一致！");
				}
			}
		});
		textNewPassword.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_3 = new Label(composite_1, SWT.NONE);
		label_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_3.setText("重输新密码");
		
		textNewPasswordAgain = new Text(composite_1, SWT.BORDER | SWT.PASSWORD);
		textNewPasswordAgain.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				if(textNewPasswordAgain.getText().trim().equals("")){
					return;
				}
				
				if(textNewPassword.getText().trim().equals(textNewPasswordAgain.getText().trim())){
					ChangePasswordDialog.this.setErrorMessage(null);
					ChangePasswordDialog.this.getButton(IDialogConstants.OK_ID).setEnabled(true);
				}else{
					ChangePasswordDialog.this.getButton(IDialogConstants.OK_ID).setEnabled(false);
					ChangePasswordDialog.this.setErrorMessage("两次输入的密码不一致！");
				}
			}
		});
		textNewPasswordAgain.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		this.setTitle("修改管理员密码");
		this.setMessage("输入正确的验证信息", IMessageProvider.INFORMATION);
		
		return composite;
	}

	@Override
	protected Point getInitialSize() {
		// TODO Auto-generated method stub
		return new Point(320,351);
	}

	@Override
	protected void configureShell(Shell newShell) {
		// TODO Auto-generated method stub
		newShell.setText("修改密码");
		super.configureShell(newShell);
	}
	
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		// TODO Auto-generated method stub
		super.createButtonsForButtonBar(parent);
		
		this.getButton(IDialogConstants.OK_ID).setEnabled(false);
	}

	@Override
	protected void okPressed() {
		// TODO Auto-generated method stub
		username = textUsername.getText().trim();
		password = textOldPassword.getText().trim();
		newpassword = textNewPassword.getText().trim();
		newpasswordagain = textNewPasswordAgain.getText().trim();
		super.okPressed();
	}

}
